package com.vic.mvc.testsome;

import java.io.File;
import java.io.FileFilter;
import java.util.HashMap;
import java.util.Map;
/**
 * 获取盘符下的相同后缀的个数和字节大小
 * @author VIC
 *
 */
public class T1 {

	/**存放文件后缀 对应的大小*/
	private static final Map<String, Long> sizeMap = new HashMap<String, Long>();
	/**存放文件后缀 对应的个数*/
	private static final Map<String, Integer> countMap = new HashMap<String, Integer>();
	
	
	public static void main(String[] args) {
		String path = "F:";
		filter(path);
		
		for(String key :sizeMap.keySet()) {
			System.out.println("后缀:" + key + "\t字节:" + (sizeMap.get(key)==null?0:sizeMap.get(key)) + "\t个数为" + (countMap.get(key)==null?0:countMap.get(key)));
		}
		
	}
	
	
	public static void filter(String path){
		File file = new File(path);
		file.listFiles(new FileFilter() {
			@Override
			public boolean accept(File f) {
				if(f.isDirectory()) {
					filter(f.getPath());
					return false;
				}
				String fileName = f.getName();
				
				if(fileName.indexOf(".") == -1) {
					return false;
				}
				
				String suffix =fileName.split("\\.")[1];//获得文件后缀
				//把文件后缀相同的字节数相加
				Long size = (sizeMap.get(suffix)==null?0:sizeMap.get(suffix)) + f.length();
				sizeMap.put(suffix, size);
				//把文件后缀相同的个数相加
				Integer count = (countMap.get(suffix)==null?0:countMap.get(suffix)) + 1;			
				countMap.put(suffix, count);
				return false;
			}
		});
	}
}

